package com.ssy.lingxi.message.serviceimpl;

import com.ssy.lingxi.common.response.ResponseCode;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.member.merchant.api.feign.inner.MemberDetailFeign;
import com.ssy.lingxi.member.merchant.api.model.vo.inner.response.MemberFeignMsgByMemberIdVO;
import com.ssy.lingxi.member.merchant.api.model.vo.inner.response.MemberFeignMsgByRoleIdVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 会员内部接口服务类
 * @author yzc
 * @version 2.0.0
 * @date 2021/7/7
 */
@Slf4j
@Service
public class MemberFeignService {

    @Resource
    private MemberDetailFeign memberDetailFeign;


    public List<Long> getUserByMemberIds(Long memberId) {
        if (Objects.isNull(memberId)) {
            return Collections.emptyList();
        }
        Wrapper<List<MemberFeignMsgByMemberIdVO>> result = memberDetailFeign.getUserByMemberIds(Collections.singletonList(memberId));
        if (result.getCode() != ResponseCode.SUCCESS.getCode()) {
            log.error("调用会员服务失败, 根据会员id查询用户信息");
            return Collections.emptyList();
        }

        return result.getData().stream().map(MemberFeignMsgByMemberIdVO::getUserId).collect(Collectors.toList());
    }

    public List<MemberFeignMsgByRoleIdVO> getUserByRoleIds(List<Long> roleIds) {
        if (CollectionUtils.isEmpty(roleIds)) {
            return Collections.emptyList();
        }
        Wrapper<List<MemberFeignMsgByRoleIdVO>> result = memberDetailFeign.getUserByRoleIds(roleIds);
        if (result.getCode() != ResponseCode.SUCCESS.getCode()) {
            log.error("调用会员服务失败, 根据角色id查询用户信息");
            return Collections.emptyList();
        }

        return result.getData();
    }
}
